package com.yunda.nebula.gateway.controller;


import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.yunda.nebula.core.tool.utils.BeanUtil;
import com.yunda.nebula.gateway.core.entity.Result;
import com.yunda.nebula.gateway.core.entity.TbGatewayPredicates;
import com.yunda.nebula.gateway.entity.dto.PredicatesDTO;
import com.yunda.nebula.gateway.listener.ConfigDeleteEvent;
import com.yunda.nebula.gateway.listener.ConfigUpdateEvent;
import com.yunda.nebula.gateway.service.ITbGatewayPredicatesService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.ApplicationContext;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;

import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author zhiguo.dong
 * @since 2020-09-17
 */
@RestController
@RequestMapping("/nebula/gateway/predicates")
public class TbGatewayPredicatesController {

    @Autowired
    private ITbGatewayPredicatesService predicatesService;

    @Autowired
    private ApplicationContext context;
    
    @RequestMapping("/insert.do")
    public Result insert(@RequestBody PredicatesDTO predicatesDTO){
        TbGatewayPredicates tbGatewayPredicates = BeanUtil.copy(predicatesDTO, TbGatewayPredicates.class);
        predicatesService.save(tbGatewayPredicates);
        context.publishEvent(new ConfigUpdateEvent(predicatesDTO.getAppId()));

        return Result.data(null);
    }

    @RequestMapping("/delete.do")
    public Result delete(@RequestParam(value = "appId") String appId){
        predicatesService.remove(new LambdaQueryWrapper<TbGatewayPredicates>().eq(TbGatewayPredicates::getAppId,appId));
        context.publishEvent(new ConfigDeleteEvent(appId));
        return Result.data(null);
    }

    @RequestMapping("/update.do")
    public Result update(@RequestBody PredicatesDTO predicatesDTO){
        TbGatewayPredicates tbGatewayPredicates = BeanUtil.copy(predicatesDTO, TbGatewayPredicates.class);
        predicatesService.update(tbGatewayPredicates,new LambdaQueryWrapper<TbGatewayPredicates>().eq(TbGatewayPredicates::getAppId,predicatesDTO.getAppId()));
        context.publishEvent(new ConfigUpdateEvent(predicatesDTO.getAppId()));
        return Result.data(null);
    }

    @RequestMapping("/get.do")
    public Result get(@RequestParam(value = "appId") String appId){
        TbGatewayPredicates tbGatewayPredicates = predicatesService.getOne(new LambdaQueryWrapper<TbGatewayPredicates>().eq(TbGatewayPredicates::getAppId, appId));
        return Result.data(tbGatewayPredicates);
    }

    @RequestMapping("/list.do")
    public Result list(@RequestParam(value = "appId" ,required = false) String appId){
        LambdaQueryWrapper<TbGatewayPredicates> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(TbGatewayPredicates::getAppId,appId);
        List<TbGatewayPredicates> list = predicatesService.list(wrapper);
        return Result.data(list);
    }

}

